package com.lmk.product.service.impl;

import com.lmk.basic.exception.BusinessException;
import com.lmk.basic.service.IBaiduAiAuditService;
import com.lmk.basic.service.Impl.BaseServiceImpl;
import com.lmk.basic.util.LoginContext;
import com.lmk.org.domain.Employee;
import com.lmk.org.mapper.EmployeeMapper;
import com.lmk.product.domain.Product;
import com.lmk.product.domain.ProductDetail;
import com.lmk.product.domain.ProductOnlineAuditLog;
import com.lmk.product.mapper.ProductDetailMapper;
import com.lmk.product.mapper.ProductMapper;
import com.lmk.product.mapper.ProductOnlineAuditLogMapper;
import com.lmk.product.service.IProductService;
import com.lmk.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void onsale(List<Long> ids, HttpServletRequest request) {
        for (Long id : ids) {
            Product product = productMapper.loadById(id);
            if(product.getState()==1){
                throw new BusinessException("已上架");
            }
        }
        //遍历集合
        for (Long id : ids) {
            //查询每一条数据
            Product product = productMapper.loadById(id);
            //对文本的审核
            Boolean aBoolean = iBaiduAiAuditService.textAudit(product.getName());
            //对图片的审核
            Boolean aBoolean1 = iBaiduAiAuditService.imageAudit(product.getResources());
            //两个都审核成功
            if(aBoolean && aBoolean1){
                //数据库操作
                Map<String,Object> params = new HashMap<>();
                params.put("id",id);
                // 上架时间
                params.put("onsaletime",new Date());
                productMapper.onsale(params);


                //操作审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setState(1);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核成功");
                productOnlineAuditLogMapper.save(log);
            }else{//审核失败
                //操作审核日志
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setState(1);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
                log.setAuditId(employee.getId());
                log.setAuditTime(new Date());
                log.setNote("审核失败");
                productOnlineAuditLogMapper.save(log);
                throw new BusinessException("内容非法,审核失败");
            }
        }

    }

    @Override
    public void offsale(List<Long> ids) {
        //下架：修改state为0，offsaletime为当前时间 ,可以用批量操作
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("ids",ids);
        hashMap.put("offsaletime",new Date());
        productMapper.offsale(hashMap);
    }



    @Override
    @Transactional
    public void add(Product product) {
        //对创建时间进行处理
        product.setCreatetime(new Date());
        super.add(product);
        //处理明细信息
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }

    }

    @Override
    @Transactional
    public void update(Product product) {

        super.update(product);
        //处理明细信息
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);
        }


    }

    @Override
    @Transactional
    public void del(Long id) {
        super.del(id);
        productDetailMapper.delPetDetailByproducttId(id);

    }

    @Override
    @Transactional
    public void batchdel(List<Long> ids) {
        super.batchdel(ids);
        productDetailMapper.bacthdelByproducttId(ids);
    }


}
